import Cookies from 'js-cookie'
import CONSTANTS from '@/config/constants'
import util from '@/libs/util'
import mixin from '../mixin'

export default {
    props: ['baseuser'],
    mixins: [mixin],
    data() {
        return {
            generateData: {
                name: '',
                putWorker: '',
                orgId: -1,
                diplomaModals: []
            },
            organizeList: [],
            diplomaModalVisible: false,
            diplomaFormData: {
                name: '',
                level: 0
            }
        }
    },
    mounted() {
        if (this.baseuser.type !== 2) {
            util.ajax.post(CONSTANTS.ORG_LIST, util.toFormData()).then(res => {
                this.organizeList = res.data.data;
            });
        }
    },
    methods: {
        // 新增奖项
        saveDiploma() {
            if (this.diplomaFormData.name.trim() === '') {
                this.$Modal.warning({
                    title: '提示',
                    content: '请填写奖项名称'
                });
            } else {
                this.generateData.diplomaModals.push(Object.assign({}, this.diplomaFormData))
                this.diplomaFormData.name = ''
                this.diplomaFormData.level = 0
                this.diplomaModalVisible = false
            }
        },
        async save() {
            this.showLoading()
            try {
                if (!this.generateData.name) {
                    throw '请输入主题名称';
                }
                if (this.generateData.diplomaModals.length <= 0) {
                    throw '请添加奖项';
                }
                const req = {
                    name: this.generateData.name,
                    putWorker: this.generateData.putWorker,
                    parentId: ''
                }

                if (this.baseuser.type !== 2) {
                    req.orgId = this.generateData.orgId
                } else {
                    req.orgId = this.baseuser.orgId
                }

                const themeSaveResult = await util.ajax.post(CONSTANTS.THEME_SAVE, util.toFormData(req))
                const diplomasSaved = themeSaveResult.data.message === 'SUCCESS'
                this.generateData.diplomaModals.forEach(async modal => {
                    const diplomaReq = {
                        themeId: themeSaveResult.data.data.id,
                        orgId: req.orgId,
                        name: modal.name,
                        level: modal.level
                    }
                    await util.ajax.post(CONSTANTS.DIPLOMAS_SAVE, util.toFormData(diplomaReq))
                })
                this.$Spin.hide()

                return { success: true, themeId: themeSaveResult.data.data.id, themeName: this.generateData.name }
                // return true
            } catch (e) {
                this.$Spin.hide()
                alert(e)
                return { success: false }
            }
        }
    },
    computed: {
    },
    beforeDestroy() {
    }
};